<template>
  <div>
    <div class="container">
      <div class="head">
        <span>同类商品推荐</span>
      </div>
      <div class="carousel">
        <ul>
          <li>
            <div class="slider">
              <a
                href="javascript:;"
                v-for="(item, index) in relevantList.slice(n, n + 4)"
                :key="index"
              >
                <img :src="item.picture" alt="" />
                <p>{{ item.name }}</p>
                <p>{{ item.price }}</p>
              </a>
            </div>
          </li>
        </ul>
        <a href="javascript:;" class="left" @click="left()">左</a>
        <a href="javascript:;" class="right" @click="right()">右</a>
        <div class="page">
          <span></span>
          <span></span>
          <span></span>
          <span></span>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      relevantList: [],
      n: 0,
    };
  },
  methods: {
    left() {
      this.n -= 4;
      if (this.n == -4) {
        this.n = 12;
      }
    },
    right() {
      this.n += 4;
      if (this.n == 16) {
        this.n = 0;
      }
    },
  },
  mounted() {
    this.axios
      .get(
        "https://apipc-xiaotuxian-front.itheima.net/goods/relevant?id=3380014&limit=16"
      )
      .then((res) => {
        if (res.data.msg == "操作成功") {
          this.relevantList = res.data.result;
          // console.log(this.relevantList.slice(0, 4));
        }
      });
  },
};
</script>
<style  lang='scss' scoped>
// .active {
//   background: #27ba9b;
// }
.container {
  background: #fff;
}
.head {
  height: 80px;
  line-height: 80px;
  padding: 0 20px;

  span {
    font-size: 20px;
    padding-left: 10px;
  }
}
.carousel {
  position: relative;
  height: 380px;

  ul {
    width: 100%;
    height: 100%;

    .fase {
      opacity: 1;
      z-index: 1;
    }

    li {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 1;
      transition: opacity 0.5s linear;

      .slider {
        display: flex;
        justify-content: space-around;
        padding: 0 40px;

        a {
          width: 240px;
          text-align: center;

          img {
            padding: 20px;
            width: 230px !important;
            height: 230px !important;
          }

          p {
            text-overflow: ellipsis;
            overflow: hidden;
            white-space: nowrap;

            &:nth-of-type(1) {
              font-size: 16px;
              color: #666;
              padding: 0 40px;
            }

            &:nth-of-type(2) {
              font-size: 16px;
              color: #cf4444;
              margin-top: 15px;
            }
          }
        }
      }
    }
  }

  .page {
    position: absolute;
    left: 0;
    bottom: 20px;
    z-index: 2;
    width: 100%;
    text-align: center;

    .active {
      background: #27ba9b;
    }

    span {
      display: inline-block;
      width: 12px;
      height: 12px;
      background: rgba(0, 0, 0, 0.2);
      border-radius: 50%;
      cursor: pointer;
      margin-left: 12px;
    }
  }

  .left {
    width: 44px;
    height: 44px;
    background: rgba(0, 0, 0, 0.2);
    color: #fff;
    border-radius: 50%;
    position: absolute;
    top: 228px;
    z-index: 2;
    text-align: center;
    line-height: 44px;
    opacity: 0;
    transition: all 0.5s;
    top: 110px;
    opacity: 1;
    background: transparent;
    color: #ddd;
    left: 20px;
  }

  .right {
    right: 20px;
    width: 44px;
    height: 44px;
    background: rgba(0, 0, 0, 0.2);
    color: #fff;
    border-radius: 50%;
    position: absolute;
    top: 228px;
    z-index: 2;
    text-align: center;
    line-height: 44px;
    opacity: 0;
    transition: all 0.5s;
    top: 110px;
    opacity: 1;
    background: transparent;
    color: #ddd;
  }
}
</style>